package com.xiaohujr.xuexue.leetcode.meituan;

public class MaxProduct {
    public int maxProduct(int[] nums) {
        int[][] dp = new int[nums.length][2];

        for (int i = 0; i < nums.length; i++) {
            dp[i][0] = 1;
            dp[i][1] = 1;
        }
        if (nums[0] > 0) {
            dp[0][1] = nums[0];
        } else {
            dp[0][0] = nums[0];
        }
        int rtn = nums[0];
        for (int i = 1; i < nums.length; i++) {
            dp[i][1] = Math.max(dp[i - 1][1] * nums[i], dp[i - 1][0] * nums[i]);
            dp[i][0] = Math.min(dp[i - 1][0] * nums[i], dp[i - 1][1] * nums[i]);
            if (rtn < dp[i][1]) {
                rtn = dp[i][1];
            }
        }
        return rtn;
    }

    public static void main(String[] args) {
        new MaxProduct().maxProduct(new int[]{3, -1, 4});
    }
}
